home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyHandleFile.p < prev    next >
Encoding:
Text File  |  1994-10-05  |  2.6 KB  |  115 lines  |  [TEXT/PJMM]

  1. unit MyHandleFile;
  2.  
  3. interface
  4.  
  5.     uses
  6.         MyTypes;
  7.  
  8.     type
  9.         HandleFile = record
  10.                 data: handle; { The data }
  11.                 pos: longInt; { current position in handle }
  12.                 crlf: CRLFTypes; { Only used for output, reading will handle either case }
  13.                 error: OSErr; { Cumulative error }
  14.             end;
  15. { You are free to modify any and all fields, keep 0<=pos<GetHandleSize(data) }
  16.  
  17.     procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
  18.     procedure DestroyHandleFile (var hf: HandleFile);
  19.     procedure WriteToHandleFile (var hf: HandleFile; s: str255); { Write string at pos into data }
  20.     function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longInt): boolean;
  21.     function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
  22. { Read string from pos in data, update pos, return true unless eohandle }
  23.  
  24. implementation
  25.  
  26. {$IFC undefined THINK_Pascal}
  27.     uses
  28.         Memory, ToolUtils;
  29. {$ENDC}
  30.  
  31.     procedure CreateHandleFile (var hf: HandleFile; le: CRLFTypes);
  32.     begin
  33.         with hf do begin
  34.             data := NewHandle(0);
  35.             pos := 0;
  36.             crlf := le;
  37.             error := MemError;
  38.         end;
  39.     end;
  40.  
  41.     procedure DestroyHandleFile (var hf: HandleFile);
  42.     begin
  43.         DisposeHandle(hf.data);
  44.         hf.data := nil;
  45.     end;
  46.  
  47.     procedure WriteToHandleFile (var hf: HandleFile; s: str255);
  48.         var
  49.             ret: longInt;
  50.     begin
  51.         with hf do begin
  52.             case crlf of
  53.                 CL_CR: 
  54.                     s := concat(s, cr);
  55.                 CL_LF: 
  56.                     s := concat(s, lf);
  57.                 CL_CRLF: 
  58.                     s := concat(s, cr, lf);
  59.             end;
  60.             ret := Munger(data, pos, nil, 0, @s[1], length(s));
  61.             if ret >= 0 then
  62.                 pos := ret
  63.             else if error = noErr then
  64.                 error := ret;
  65.         end;
  66.     end;
  67.  
  68.     function ReadLongLineFromHandle (var hf: HandleFile; var off, len: longInt): boolean;
  69.         var
  70.             size: longInt;
  71.             p: ptr;
  72.     begin
  73.         with hf do begin
  74.             size := GetHandleSize(data);
  75.             ReadLongLineFromHandle := pos < size;
  76.             off := pos;
  77.             p := ptr(ord(data^) + pos);
  78.             while (pos < size) & (p^ <> 13) & (p^ <> 10) do begin
  79.                 pos := pos + 1;
  80.                 longInt(p) := longInt(p) + 1;
  81.             end;
  82.             len := pos - off;
  83.             if (pos < size) & (p^ = 13) then begin
  84.                 pos := pos + 1;
  85.                 longInt(p) := longInt(p) + 1;
  86.             end;
  87.             if (pos < size) & (p^ = 10) then begin
  88.                 pos := pos + 1;
  89.                 longInt(p) := longInt(p) + 1;
  90.             end;
  91.         end;
  92.     end;
  93.  
  94.     function ReadFromHandleFile (var hf: HandleFile; var s: str255): boolean;
  95.         var
  96.             off, len: longInt;
  97.             p: ptr;
  98.             gotline: boolean;
  99.     begin
  100.         s := '';
  101.         gotline := ReadLongLineFromHandle(hf, off, len);
  102.         if gotline then begin
  103.             p := ptr(ord(hf.data^) + off);
  104.             if len > 255 then
  105.                 len := 255;
  106. {$PUSH}
  107. {$R-}
  108.             s[0] := chr(len);
  109.             BlockMove(p, @s[1], len);
  110. {$POP}
  111.         end;
  112.         ReadFromHandleFile := gotline;
  113.     end;
  114.  
  115. end.